 <template>
   <el-card shadow="always" class="box-card">
      <div class="card-name search-item">
        <div class="name width2">员工管理</div>
        <div class="mr-15">
          <el-button size="medium" type="primary" @click="add">新 增</el-button>
          <el-button size="medium" type="danger">刷 新</el-button>
        </div>
      </div>
      <div class="content-con">
        <div class="form-item">
            <el-table
              :data="tableData.slice((currentPage-1)*pagesize,currentPage*pagesize)"
              class="table"
              stripe border
              :header-cell-style="{background:'#f3f3f3',textAlign: 'center'}"
              :cell-style="{ textAlign: 'center' }"><!--:data="用于存放请求数据回来的数组" -->
              <el-table-column prop="staff_no" label="职员编号">
              </el-table-column>
              <el-table-column prop="staff_name" label="职员名称">
              </el-table-column>
              <el-table-column prop="status" label="状态">
                <template slot-scope="scope">
                  <el-tag v-if="scope.row.status == 0" type="success">已启用</el-tag>
                  <el-tag v-else type="warning">已禁用</el-tag>
                </template>
              </el-table-column>
              <el-table-column label="操作">
                <template slot-scope="scope">
                  <el-button type="text" @click="handleEdit(scope.row)">
                    <i class="el-icon-edit"></i>编辑
                  </el-button>
                  <el-button type="text" @click.native.prevent="deleteRow(scope.$index,tableData)">
                    <i class="el-icon-delete"></i>删除
                  </el-button>
                </template>
              </el-table-column>
            </el-table>
          </div>
        <!-- 分页-页码 -->
        <div class="page">
          <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="currentPage"
            :page-sizes="[5, 10, 15, 20]"
            :page-size="pagesize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="tableData.length">
          </el-pagination>
        </div>
      </div>
      <el-dialog
        :title="text"
        :visible.sync="dialogAdd"
        width="30%">
        <el-form :model="addForm" ref="addForm" size="medium" label-width="100px">
          <el-form-item label="职员编号:" prop="staff_no">
            <el-input v-model="addForm.staff_no" placeholder="请输入职员编号"></el-input>
          </el-form-item>
          <el-form-item label="职员名称:" prop="staff_name">
            <el-input v-model="addForm.staff_name" placeholder="请输入职员名称"></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button type="success" @click="save">保 存</el-button>
          <el-button @click="close">取 消</el-button>
        </span>
      </el-dialog>
   </el-card>

 </template>
<script>

export default {
  data () {
    return {
      dialogAdd: false,
      currentPage: 1, //当前页码
      pagesize: 5,    //每页的数据条数
      //tableData: [] //表格数据
      text:'',
      tableData: [
        {
          staff_no: 'wsl003',
          staff_name: '副总裁03',
          status: '0'
        }
      ],
      addForm: {
        staff_no: '',
        staff_name: ''
      }
    }
  },

  methods: {
    //保存表单
    save() {
      this.$refs['addForm'].validate((valid) => {
        if (valid) {
          console.log(this.addForm)
          this.addForm = {
            staff_no:"",
            staff_name:""
          }
          console.log(this.addForm)
          this.dialogAdd = false;
          this.$message.success('成功!');
        } else {
          console.log('error submit!!');
          this.$message.error('失败!');
          return false;
        }
      });
    },
    //对话框-取消/关闭
    close() {
      this.dialogAdd = false;
      this.addForm = {
        staff_no:"",
        staff_name:""
      }
    },

    //编辑操作
    handleEdit(row) {
      this.dialogAdd = true;
      this.text= "修改职员"
      this.addForm.staff_no = row.staff_no;
      this.addForm.staff_name = row.staff_name;
    },

    //新增
    add() {
      this.dialogAdd = true;
      this.text = "新增职员"
    },

    //删除操作
    deleteRow(index, rows){
      rows.splice(index, 1);
    },

    //每页条数改变时触发事件,选择一页显示多少行
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.currentPage = 1;
      this.pagesize = val;
    },
    // 当前页改变时触发 跳转其他页
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.currentPage = val;
    }
  }
}
</script>
<style lang="scss">

</style>
